package com.lanou.moving.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou.moving.R;
import com.lanou.moving.activity.MVPlayActivity;
import com.lanou.moving.adapter.SingerMVAdapter;
import com.lanou.moving.base.BaseFragment;
import com.lanou.moving.constant.URL;
import com.lanou.moving.entity.SingerMV;
import com.lanou.moving.volley.StringRequestUtf;
import com.lanou.moving.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/24.
 */
public class SingerMvFragment extends BaseFragment implements AdapterView.OnItemClickListener {
    private StringRequestUtf request;
    private VolleySingleton volleySingleton;
    private ListView singer_mv_listview;
    private SingerMVAdapter adapter;
    private int singer_id;
    private List<SingerMV.DataEntity> mvs;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_singer_mv, null);
        singer_mv_listview = (ListView) view.findViewById(R.id.singer_mv_listview);
        singer_mv_listview.setOnItemClickListener(this);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance(mContext);
        Bundle bundle = getArguments();
        singer_id = bundle.getInt("singer_id");
        getData();
    }

    private void getData() {
        String url = URL.SINGER_MV_START + singer_id + URL.SINGER_MV_END;
        request = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SingerMV singerMV = gson.fromJson(response, SingerMV.class);
                mvs = singerMV.getData();
                adapter = new SingerMVAdapter(mContext, mvs);
                singer_mv_listview.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), MVPlayActivity.class);
        intent.putExtra("videoId", (int)mvs.get(position).getMvList().get(0).getVideoId());
        startActivity(intent);
    }
}
